package cetc54.zhtx.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * 断开连接触发器
 * @author lipeng
 */
@Slf4j
public class ServerTriggerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        //判断是否为心跳包
        if(evt instanceof IdleStateEvent){
            IdleState idleState = ((IdleStateEvent) evt).state();
            if(idleState==IdleState.READER_IDLE){
                //在规定时间内没有收到心跳包 断开连接
                ctx.disconnect();
                ctx.close();
                log.info("超时未收到主机心跳---切换备机工作");
            }

        }
    }



}
